$(function(){
    var tag = false;
    //用户id=1，企业id=2
    $("#forgetPsw").attr("href","password-reset.html?type=0&id=2");

    // //切换登录
    $(".switch-tab li").click(function () {
        $(this).siblings('li').removeClass('curr-tab');
        $(this).addClass('curr-tab');
        //个人登录
        if($(".switch-tab li:first-child").hasClass('curr-tab')){
            tag = true;
            $("#forgetPsw").attr("href","password-reset.html?type=0&id=1");
        }else{
            //企业操作
            tag = false;
            $("#forgetPsw").attr("href","password-reset.html?type=0&id=2");
        }
    });
    

    //获取验证码
    $("#getCode").click(function(){
        console.log(tag)
        var tel = $('#tel').val();
        if(telchek(tel)==false){
            return false;
        }
        setTime(this)
        if (tag==false) {
            loginUrl = companyServices + "/user/vcode/login/"+tel;
        } else {
            loginUrl = services + "/user/vcode/login/"+tel;
        }
        // url = services + "/user/vcode/login/"+tel;
        $.ajax({
            type: 'get', // 提交方式 get/post
            url:loginUrl,// 需要提交的 url
            contentType:"application/json;charset=utf-8",
            xhrFields: {
                withCredentials: true
            },
            crossDomain: true,
            success: function(res) {
                if(res.status=="00200"){
                    layerMsg(res.message)
                }else{
                    layerMsg("获取验证码失败!请重试")
                }
            },
        })
    })
 


    //点击登录
    $("#loginBtn").click(function () {

        var tel = $('#tel').val();
        var psw = $('#psw').val();
        var verifyCode = $('#verifyCode').val();
        if(telchek(tel)==false){
            return false;
        }
        //密码登录
        if(psw!=="" && verifyCode==""){
            if(psw.length<6){
                layerMsg("密码为6位或6位以上");
                return false;
            }
            var url = '';

            if (tag==false) {
                url = companyServices+"/user/pubKey";
            } else {
                url = services+"/user/pubKey";
            }
    
            $.ajax({
                url:url
                ,type:"get"
                ,xhrFields: {
                    withCredentials: true
                }
                ,crossDomain: true
                ,success:function(result){
                    var status = result.success;
                    if(status == false){
                        layerMsg(result.message);
                    }
                    var publicKey = result.data.publicKey;
                    //创建加密算法对象
                    var encrypt = new JSEncrypt();
                    encrypt.setPublicKey(publicKey);
                    psw = encrypt.encrypt(psw);
                    var loginUrl = '';
                    if (tag==false) {
                        loginUrl = companyServices + "/user/login";
                    } else {
                        loginUrl = services+"/user/login";
                    }
                    $.ajax({
                        url: loginUrl
                        ,type:"post"
                        ,xhrFields: {
                            withCredentials: true
                        }
                        ,crossDomain: true
                        ,contentType:"application/json;charset=utf-8"
                        ,data:	JSON.stringify({
                            "username":tel
                            ,"password":psw
                        })
                        ,success:function(res){
                            console.log(res)
                            if(res.status!='00200'){
                                layerMsg(res.message);
                            }else{
                                layerMsg(res.message);
                                var userInfo = JSON.stringify(res.data);
                                $.cookie('userInfo', userInfo);
                                setTimeout(function() {
                                    if (tag==false) {
                                        //如果是企业用户，判断是否有公司信息
                                        var companyId = res.data.companyId;
                                        loadHrAuditStatus(companyId);
                                    } else {
                                       
                                        if($.cookie('curren_url')){
                                            var _url = $.cookie('curren_url')
                                             $.cookie('curren_url',null,{ expires: -1 })
                                            window.location.href = _url
                                        }else{
                                            window.location.href="personal/my-resume.html" ;
                                        }
                                        
                                    }
    
                                },1000);
                            }
                        }
                    });
                }
            });
        }

        //验证码登录
        if(psw=="" &&verifyCode!==""){
            var loginUrl = '';
            if (tag==false) {
                loginUrl = companyServices + "/user/phoneLogin";
            } else {
                loginUrl = services+"/user/phoneLogin";
            }
            $.ajax({
                url: loginUrl
                ,type:"post"
                ,xhrFields: {
                    withCredentials: true
                }
                ,crossDomain: true
                ,contentType:"application/json;charset=utf-8"
                ,data:	JSON.stringify({
                    "username":tel
                    ,"verifyCode":verifyCode
                })
                ,success:function(res){
                    if(res.status!='00200'){
                        layerMsg(res.message);
                    }else{
                        layerMsg(res.message);
                        var userInfo = JSON.stringify(res.data);
                        $.cookie('userInfo', userInfo);
                        setTimeout(function() {
                            if (tag==false) {
                                //如果是企业用户，判断是否有公司信息
                                var companyId = res.data.companyId;
                                loadHrAuditStatus(companyId);
                            } else {
                                window.location.href="personal/my-resume.html" ;
                            }
                        },1000);
                    }
                }
            });
        }

    });
});


//加载公司信息
function loadCompany() {
    $.ajax({
        type: 'get', // 提交方式 get/post
        url: companyServices+"/company", // 需要提交的 url
        contentType:"application/json;charset=utf-8",
        xhrFields: {
            withCredentials: true
        }
        ,crossDomain: true
        ,data:{},
        success: function(res) {
            checkLoginStatus(res);

            if(res.status == '00200'){
                var licenseImg = res.data.licenseImg;

                if (licenseImg != null && licenseImg != '' && licenseImg != 'undefined') { //跳转首页
                    if (isMobile()) {
                        window.location.href = "company/wrap-company-backstage.html";
                    } else {
                        window.location.href = "company/job-manage.html";
                    }
                } else {
                    window.location.href="company/regi-charge-info.html"; //补填
                }
            } else { //加载公司信息失败，跳到填公司信息
                window.location.href="company/regi-company-info.html"; //补填
            }
        }
    })
}


function loadHrAuditStatus(companyId) {
    $.ajax({
        type: 'get', // 提交方式 get/post
        url: companyServices+"/user/join/company/audit", // 需要提交的 url
        contentType:"application/json;charset=utf-8",
        xhrFields: {
            withCredentials: true
        }
        ,crossDomain: true
        ,data:{},
        success: function(res) {
            checkLoginStatus(res);

            if(res.status == '00200'){
                if (res.data != null) { //有申请记录，是加入hr
                    if (companyId != null && companyId != '' && companyId != 'undefined') { //跳转首页
                        if (isMobile()) {
                            window.location.href = "company/wrap-company-backstage.html";
                        } else {
                            window.location.href = "company/job-manage.html";
                        }
                    } else {
                        layerMsg("您的申请还未通过，请联系相关人员，进行审核操作。");
                        setTimeout(function () {
                            logoutCompany();
                        }, 2000)
                    }
                } else {
                    if (companyId != null && companyId != '' && companyId != 'undefined') { //跳转首页
                        loadCompany();
                    } else { //跳转去填公司信息
                        window.location.href="company/regi-company-info.html"; //补填
                    }
                }
            } else { //加载公司信息失败，跳到填公司信息
                if (companyId != null && companyId != '' && companyId != 'undefined') { //跳转首页
                    loadCompany();
                } else { //跳转去填公司信息
                    window.location.href="company/regi-company-info.html"; //补填
                }
            }
        }
    })
}


function wayswitch(obj){
    className = obj.className;
    console.log(className)
    
    if(className=="code-login"){
        contraryClassName = "psw-login";
    }else{
        contraryClassName = "code-login";
    }
    console.log(contraryClassName)
    $("."+className).css("display","none");
    $("."+className).find("input[type='text']").val("");
    $("."+className).find("input[type='password']").val("");
    $("."+contraryClassName).css("display","block");
}
//登录验证码
